package com.mk.dynamic.redis;

import java.util.Map;
import java.util.HashMap;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;

public class YamlDynamicRedisProvider implements DynamicRedisProvider{

    private DynamicRedisProperties properties;

    public YamlDynamicRedisProvider(DynamicRedisProperties properties) {
        this.properties = properties;
    }

    @Override
    public Map<String, RedisConnectionFactory> loadRedis() {
        // 获取多个Redis配置参数
        Map<String, RedisProperties> connection = this.properties.getConnection();
        Map<String, RedisConnectionFactory> connectionMap = new HashMap<>(connection.size());
        connection.forEach((key, value)-> {
            connectionMap.computeIfAbsent(key, k -> JedisConnectionFactoryBuilder.build(value));
        });

        return connectionMap;
    }
}
